package com.jp.knowledge.view;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

import com.jp.knowledge.adapter.AttentionAdapter;
import com.jp.knowledge.util.L;

import java.util.ArrayList;
import java.util.List;

public class TabHostWrapPagerView extends ViewPager {

    private AttentionAdapter attentionAdapter;
    private RecyclerView recyclerView;
    private Context mContext;
    public TabHostWrapPagerView(Context context) {
        super(context);
        this.mContext = context;
    }

    public TabHostWrapPagerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    /**
     * @param recycleView
     * @param attentAdapter
     * @param fm      Frame管理器
     * @param list    展示Fragment集合
     */
    public void init(RecyclerView recycleView, AttentionAdapter attentAdapter, FragmentManager fm, List<Fragment> list) {
        this.recyclerView=recycleView;
        this.attentionAdapter =attentAdapter;
        if(attentionAdapter!=null) {
            attentAdapter.setOnItemSelect(new AttentionAdapter.OnItemSelect() {
                @Override
                public void onItemSelect(int position) {
                    setCurrentItem(position);
                }
            });
        }
        if (list.isEmpty()) return;
        this.setAdapter(new TabFragmentAdapter(fm, list));
        this.setOnPageChangeListener(mPageChange);
    }


    /**
     * 卡片内容切换监听
     */
    OnPageChangeListener mPageChange = new OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            L.e("positionOffset="+positionOffset+"--------positionOffsetPixels==="+positionOffsetPixels);
        }

        @Override
        public void onPageSelected(int position) {
//            if (position != 0) {
                L.e("attentionAdapter.getItemWidth()="+attentionAdapter.getItemWidth());
//                recyclerView.scrollTo(attentionAdapter.getItemWidth()*(position-1),0);
//                recyclerView.scrollToPosition(attentionAdapter.getItemWidth()* (position - 1), 0);
                recyclerView.smoothScrollToPosition(position);
//            }
                attentionAdapter.update(position);
            if (pagerChangedListener != null)
                pagerChangedListener.pagerChanged(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

    };


    /**
     * 卡片内容适配器
     */
    public static class TabFragmentAdapter extends FragmentPagerAdapter {

        List<Fragment> fragmentList = new ArrayList<>();

        public TabFragmentAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }

    private PagerChangedListener pagerChangedListener;
    public void setPagerChangedListener(PagerChangedListener pagerChangedListener){
        this.pagerChangedListener = pagerChangedListener;
    }
    public interface PagerChangedListener{
        void pagerChanged(int position);
    }
}
